|
Direct Camera for Unreal
Direct Camera Unreal docs
|
For more about Direct Camera, look at the promo page
For an example project and a pre-build demo apk, check out https://github.com/joemarshall/directcameraexamples.
For camera access to work, you need to have the correct permissions set in your app manifest. Direct Camera can do this for you - in Project settings, Plugins, Direct Camera, you can choose what permissions you want. There are two options, one for standard cameras, and another for Meta Quest passthrough cameras, which require a different permission.
Before you can access a camera, you need to request permissions from Android to do so. This may pop up a box for the user if they haven't already allowed it, so make sure you have done this before you try opening a camera.
You can do this using the Request Direct Camera Runtime Permissions blueprint object (which calls UDirectCameraPermissionGetter::RequestDirectCameraRuntimePermissions ). This object has two output pins, which are fired asynchronously on permissions granted or refused. Wait for these to fire before you open the camera. This uses the project settings above to choose what permissions to request, so make sure you set them up correctly.
To do this, you first need to create an Unreal Media Player object and assign it to a Media Texture as usual (and put that texture on a material or whatever you want to do with it). Then, once you have requested permissions successfully, you can open the camera. You open a camera using the Open Direct Camera blueprint object (which calls UCameraCaptureSettingsLibrary::OpenDirectCamera). You can set the various inputs on the Open Direct Camera object to non-zero if you want to open a specific camera or at a specific resolution.
If you want to capture frames, you use the Create Capture Frame Buffer blueprint call (UDirectCameraFrameBuffer::CreateCaptureFrameBuffer). This creates a buffer to capture frames from the camera. The frame buffer has a format, which is a member of the DirectCameraFrameType enum, along with a width and height (leave these at zero if you just want the current resolution).
Once you have a frame buffer object, you can capture from the camera to it. To capture a frame to your code you then call Start Capture (UDirectCameraFrameCapture::StartCapture) with the frame buffer, which then fires its On Frame Buffer Ready event when the buffer is filled.
You can then access the frame data by using the methods on the Direct Camera Frame Buffer object (see UDirectCameraFrameBuffer for details.)
Once you have a frame buffer, if it is in an image format (e.g. jpeg image, DNG raw image) you may want to save it to image storage on your device. You can do this using the Save Image blueprint method (UDirectCameraFrameBuffer::SaveImage). This lets you save the file either to a path that you give it, or to the media gallery on the device (in this case you just give it the name). If you save to gallery, you don't have to worry about Android permissions. If you save to a path, then you need to be sure you have access to the path that you are writing to, either by requesting permissions, or by using a path within your application's storage directory.
Make sure you give the filename the correct extension (.jpg, .dng etc.) as bad things happen on Android if you don't.